import datetime
import sys

from PyQt5 import QtWidgets
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QApplication, QSizePolicy, QFileDialog, QTableWidgetItem
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from qt_forms.test import Ui_Form
import data_trans


# 首先定义一个继承自FigureCanvas的类
class Mydemo(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=500):
        # plt.rcParams['font.family'] = ['SimHei']
        # plt.rcParams['axes.unicode_minus'] = False
        # 创建一个2*2布局的表格
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        # 设置横纵坐标的名称以及对应字体格式
        self.axes_1 = self.fig.add_subplot(1, 2, 1)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.fig.tight_layout()

    def dis_datas(self, input: list):
        input_len = len(input)
        for i in range(input_len):
            axis = self.fig.add_subplot(input_len, 2, 2 * (i + 1))
            x, y = input[i]
            axis.plot(x, y)
            if(i<input_len-1):
                axis.set_xticks([])
        self.draw()


class MainWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

        self.__init_cavasUi()
        self.__init_action()

    def __init_cavasUi(self):
        # 创建图表的实例
        self.cavas = Mydemo(width=5, height=4, dpi=100)
        self.widget_toolbar = NavigationToolbar(self.cavas, self.widget_10)
        self.vl.addWidget(self.widget_toolbar)
        self.vl.addWidget(self.cavas)

    def __init_action(self):
        self.btn_openFile.clicked.connect(self.btn_open_file_ywzf_clicked)
        self.btn_reconginze.clicked.connect(self.btn_reconginze_clicker)

    def btn_open_file_ywzf_clicked(self):
        import numpy as np
        import cv2
        file_name = self.open_image_file()
        if file_name == '':
            return
        self.image = cv2.imread(filename=file_name)
        # 图片显示
        self.cavas.axes_1.imshow(self.image, cmap='gray')
        # 在界面上显示
        self.cavas.draw()

    def btn_reconginze_clicker(self):
        datas = data_trans.get_datas(self.image)
        self.cavas.dis_datas(datas)

    def open_image_file(self):
        """打开一个图像文件"""
        fileName, filetype = QFileDialog.getOpenFileName(self,
                                                         "open file", '.',
                                                         "jpg Files (*.jpg);;png Files (*.png);;All Files (*)")
        item = '打开文件:' + fileName
        self.addItemAndFocusIndex(item)
        return fileName

    def addItemAndFocusIndex(self, item):
        self.lst_info.addItem(item)
        self.lst_info.setCurrentRow(self.lst_info.count() - 1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    sys.exit(app.exec_())